<!DOCTYPE html PUBLIC "-//WAPFORUM//DTD XHTML Mobile 1.0//EN" "http://www.wapforum.org/DTD/xhtml-mobile10.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link href="/css/admin.css" rel="stylesheet" type="text/css"></link>
<script type="text/javascript" src="/js/jquery.js"> </script>
<title>评论列表 admin page</title>

<style type="text/css">
a.page-nav {
  font-size:14px;
  border:1px solid red;
  margin:2px;
  padding:2px;
}
a.current {
  text-decoration:none;
  border:0;
}
li {
  line-height: 32px;
}
input[type="radio"]:checked+label{color:#a22;}
</style>

</head>
<body>
  <div class="main">
    <ul>
    <?php 
    include("util.php");

$GLOBALS['THRIFT_ROOT'] = '/opt/php/lib/php/thrift-0.8.0';

// Load up all the thrift stuff
require_once $GLOBALS['THRIFT_ROOT'].'/Thrift.php';
require_once $GLOBALS['THRIFT_ROOT'].'/protocol/TBinaryProtocol.php';
require_once $GLOBALS['THRIFT_ROOT'].'/transport/TSocket.php';
require_once $GLOBALS['THRIFT_ROOT'].'/transport/TFramedTransport.php';

// Load the package that we autogenerated for this tutorial
require_once './gen-php/ServiceBase/ServiceBase.php';
require_once './gen-php/ServiceBase/ServiceBase_types.php';
require_once './gen-php/ArticleComment/ArticleComment.php';
require_once './gen-php/ArticleComment/ArticleComment_types.php';

$comment_service = null;
try {
  // Create a thrift connection (Boiler plate)
  $socket = new TSocket('127.0.0.1', '9092');
  $transport = new TFramedTransport($socket);
  $protocol = new TBinaryProtocol($transport);

  $comment_service = new ArticleCommentClient($protocol);
  $transport->open();

} catch (TException $tx) {
  echo "error<br/>";
}

$audit_state = 0;
if ($_GET['as']) {
  $audit_state = intval($_GET['as']);
}
$comments_result = $comment_service->GetComments($audit_state, 0, 20);
echo '调用结果:' . $comments_result->success . '<br/>';

          foreach($comments_result->comments as $comment) {
            echo '<li style="border-top:1px dotted #AEBACD">';
            print('id=<span class="comment_id">' . $comment->id . '</span>');
            echo ' &nbsp; ';
            if (($comment->state & 0x03) == 0) {
              echo '<span style="color:#8f2122;font-weight:bold;">未审核</span>';
            } else if (($comment->state & 0x03) == 1) {
              echo '<span style="color:red;">审核失败</span>';
            } else {
              echo '<span style="color:green;">审核通过</span>';
            }

            echo ' &nbsp; ';
            printf('<a target="_blank" href="/a/%d.html">%d</a> ', $comment->article_id, $comment->article_id);
            $article_meta = GetArticleMeta($comment->article_id);
            printf('<a target="_blank" href="/a/%d.html">%s</a>', $comment->article_id, $article_meta['a_title']);

            echo ' &nbsp; ';

            if ($comment->sender_id > 0) {
              print('<span><a href="/u/' . $comment->sender_id . '">' . $comment->sneder_name . '</a></span>' 
                  . '<span style="margin:6px;float:right;">' . date('Y-m-d H:i:s', $comment->time_stamp) . '</span>');
            } else {
              print('<span>' . $comment->sender_name . '</span>' . '<span style="margin:6px;float:right;">' 
                  . date('Y-m-d H:i:s', $comment->time_stamp) . '</span>');
            }
            print('<br /><span>' . $comment->content . '</span>');
            print('<br /><a href="#nogo" class="del-button">删除</a> &nbsp; ' . 
                  '<a href="#nogo" class="ok-button">合法</a>  &nbsp; <a href="#nogo" class="fail-button">非法</a>');
            print('</li>');
          }

    ?>
    </ul>
	</div>
<script type="text/javascript">
    $('.del-button').click(
      function() {
        if(!confirm ("确定删除此评论?")) {
          return;
        }
        var li = $(this).parent();
        var comment_id = $('.comment_id', li).text();
        $.ajax({
          url: "/comment-del.php?id=" + comment_id,
          context : $(this),
          success : function(rsp_txt) {
            $(this).text("已删除");
          }
        });
      }
    );
    $('.ok-button').click(
      function() {
        var li = $(this).parent();
        var comment_id = $('.comment_id', li).text();
        $.ajax({
          url: "/comment-audit.php?id=" + comment_id + '&as=2',
          context : $(this),
          success : function(rsp_text) {
            $(this).text("已更新");
          }, 
          error : function() {
            $(this).text('更新失败');
          }
        });
      }
    );

    $('.fail-button').click(
      function() {
        var li = $(this).parent();
        var comment_id = $('.comment_id', li).text();
        $.ajax({
          url: "/comment-audit.php?id=" + comment_id + '&as=1',
          context : $(this),
          success : function(rsp_text) {
            $(this).text("已更新");
          }, 
          error : function() {
            $(this).text('更新失败');
          }
        });
      }
    );
</script>
</body>
